import java.util.Scanner;
import java.sql.*;

/****************************************************************************** 
 * The <code>FinanceApp</code> class Prompts the user for data inputs.
 * These inputs will be used in the <code>FinanceDB</code> class to build queries.
 * @author Team 4 (Mark Colton, Brian Holley, Stacee Madsen and Chris Smith)
 *****************************************************************************/

public class FinanceApp 
{
    private FinanceDB finDB;
    private static Scanner kbReader = new Scanner(System.in).useDelimiter("\n");
    private ResultSet rs;
    
    /***************************************************************************
     * Creates a <code>FinanceDB</code> object that contains all of the 
     * finance transaction records.
     **************************************************************************/

    public FinanceApp() {
        finDB = new FinanceDB();
    }
    
    /*************************************************************************** 
    * Adds a new Deposit to the transaction table.
    **************************************************************************/ 
    public void Deposit() throws Exception{
        double Balance = 0.0;
        int maxID = finDB.GetMaxID();
        rs = finDB.GetTableRecord(maxID);
        while (rs.next()){
            Balance = rs.getDouble(6);
        }
        
        System.out.print("Enter Deposit Date:");
        String date = kbReader.next();
        System.out.print("Enter a Description (no spaces):");
        String desc = kbReader.next();
        System.out.print("Enter Deposit Amount:");
        double Deposit = kbReader.nextDouble();
        Balance+= Deposit;
        finDB.AddTableRecord(date, "DEP", desc, Deposit, Balance);
        
    }
    
    /************************************************************************** 
    * Enters a transaction for a withdrawal transaction to the transaction table.
    **************************************************************************/ 
    
    public void Withdrawal() throws Exception{
        double Balance = 0.0;
        int maxID = finDB.GetMaxID();
        rs = finDB.GetTableRecord(maxID);
        while (rs.next()){
            Balance = rs.getDouble(6);
            }
        
        System.out.print("Enter Withdrawal Date:");
        String date = kbReader.next();
        System.out.println("Enter Deposit Type:");
        System.out.println("1 = ATM");
        System.out.println("2 = Check");
        System.out.println("3 = Debit");
        System.out.println("4 = EFT");
        System.out.print("Enter your choice: ");

        int choice = kbReader.nextInt();
        String type = "0";
        if      (choice == 1) {type = "ATM";}
        else if (choice == 2) {type = "CHK";}
        else if (choice == 3) {type = "DBT";}
        else if (choice == 4) {type = "EFT";}
        System.out.print("Enter the description (no spaces):");
        String desc = kbReader.next();
        System.out.print("Enter Withdrawal Amount:");
        double Withdrawal = kbReader.nextDouble();
        Balance-= Withdrawal;
        finDB.AddTableRecord(date, type, desc, Withdrawal, Balance);
        
    }
    
    /*************************************************************************** 
    * Retrieves the current balance in the transaction table.
    **************************************************************************/ 
    
    public void getBalance() throws Exception{
        double Balance = 0.0;
        int maxID = finDB.GetMaxID();
        rs = finDB.GetTableRecord(maxID);
        while (rs.next()){
            Balance = rs.getDouble(6);
        }
        System.out.print("Your current balance is: " + Balance);
     } 
    
    /*************************************************************************** 
    * Retrieves and displays all the records in the transaction table.
    **************************************************************************/ 
    
    public void displayRegister() throws Exception {
        rs = finDB.GetTable();
        System.out.printf("%-10s %-14s %-6s %-20s %-8s %-8s\n", "Trans ID", "Date", "Type", "Desc", "Amount", "Balance");
        //System.out.println("Trans ID" + "\t" + "Date" + "\t" + "Type" + "\t" + "Desc" + "\t" + "Amount" + "\t" + "Balance");
        while(rs.next()) {
            System.out.printf("%-10s %-14s %-6s %-20s %-8.2f %-8.2f\n", rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDouble(5), rs.getDouble(6));
            //System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4) 
            //+ "\t" + rs.getDouble(5) + "\t" + rs.getDouble(6));
     }
    }
    
    /*************************************************************************** 
    * Edits records in the transaction table.
    **************************************************************************/ 
    
    public void editRecord() throws Exception {
        System.out.print("What Transaction ID to edit:");
        int transID = kbReader.nextInt();
        System.out.print("What field needs editing:");
        String field = kbReader.next();
        if (field.equalsIgnoreCase("amount")) {
            System.out.print("Enter new number value:");
            double newValue = kbReader.nextDouble();
            finDB.EditTableRecord(transID, field, newValue);
        }
        else if (field.equalsIgnoreCase("balance")) {
            System.out.print("Enter new number value:");
            double newValue = kbReader.nextDouble();
            finDB.EditTableRecord(transID, field, newValue);
        }
        else {
            System.out.print("Enter new text value:");
            String newValue = kbReader.next();
            finDB.EditTableRecord(transID, field, newValue);
        }
    }
    
    /*************************************************************************** 
    * Searches the transaction table for a specified transID and displays that record.
    **************************************************************************/ 
    
    public ResultSet searchRecord() throws Exception {
        System.out.print("What Transaction ID to Search for:");
        int transID = kbReader.nextInt();
        rs = finDB.GetTableRecord(transID);
        while(rs.next()) {
            System.out.printf("%-10s %-14s %-6s %-20s %-8.2f %-8.2f\n", rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDouble(5), rs.getDouble(6));
         //System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4) 
         //+ "\t" + rs.getDouble(5) + "\t" + rs.getDouble(6));
     }
        return rs;
    }
    
 /*       public void deleteRecord() throws Exception {
        System.out.print("What Transaction ID to delete:");
        int transID = kbReader.nextInt();
        finDB.DeleteTableRecord(transID);
        System.out.println("Transaction ID " + transID + " deleted.");   
    }*/
    
    /*************************************************************************** 
    * Interactive user menu to update and retrieve records from the transaction table.
    **************************************************************************/ 
    
    public void menu() throws Exception
    {
        int choice = 0;
        boolean loopAgain = true;

        while (loopAgain == true) {
            System.out.println("\n\nPersonal Financial Application");
            System.out.println("0 = Exit");
            System.out.println("1 = Deposit");
            System.out.println("2 = Withdrawal");
            System.out.println("3 = Get Balance");
            System.out.println("4 = Display Register");
            System.out.println("5 = Edit a Transaction");
            System.out.println("6 = Search for a Transaction");
            //System.out.println("7 = Delete a Transaction");

            System.out.print("Enter your selection: ");
            choice = kbReader.nextInt();
            
            if      (choice == 0) { loopAgain = false; }
            else if (choice == 1) { Deposit(); }
            else if (choice == 2) { Withdrawal(); }
            else if (choice == 3) { getBalance(); }
            else if (choice == 4) { displayRegister(); }
            else if (choice == 5) { editRecord(); }
            else if (choice == 6) { searchRecord(); }
           // else if (choice == 7) { deleteRecord(); }
            }
    }

    
    /*************************************************************************** 
    * Begin of the FinanceApp class. 
    * @param args A <code>String array</code> not used by this application. 
    **************************************************************************/ 

    public static void main(String [] args) throws Exception {
        FinanceApp finApp = new FinanceApp();
        finApp.menu();
    }
}

